<template>
  <div id="tags-view-container" class="tags-view-container">
    <scroll-pane ref="scrollPane" class="tags-view-wrapper" @scroll="handleScroll">
      <router-link
        v-for="tag in visitedViews"
        ref="tag"
        :key="tag.path"
        :class="isActive(tag) ? 'active' : ''"
        :to="{ path: tag.path ? tag.path : '', query: tag.query, fullPath: tag.fullPath ? tag.fullPath : '' }"
        class="tags-view-item"
        :style="activeStyle(tag)"
        @click.middle="!isAffix(tag) ? closeSelectedTag(tag) : ''"
        @contextmenu.prevent="openMenu(tag, $event)"
      >
        {{ tag.title }}
        <span v-if="!isAffix(tag)" @click.prevent.stop="closeSelectedTag(tag)">
          <icon-close />
        </span>
      </router-link>
    </scroll-pane>
    <ul v-show="visible" :style="{ left: left + 'px', top: top + 'px' }" class="contextmenu">
      <li @click="refreshSelectedTag(selectedTag)"><icon-refresh />刷新页面</li>
      <li v-if="!isAffix(selectedTag)" @click="closeSelectedTag(selectedTag)"><icon-close />关闭当前</li>
      <li @click="closeOthersTags"><icon-close-circle />关闭其他</li>
      <li v-if="!isFirstView()" @click="closeLeftTags"><icon-arrow-left />关闭左侧</li>
      <li v-if="!isLastView()" @click="closeRightTags"><icon-arrow-right /> 关闭右侧</li>
      <li @click="closeAllTags(selectedTag)"><icon-close-circle />全部关闭</li>
    </ul>
  </div>
</template>

<script lang="ts">
import ScrollPane from '../scrollPane/ScrollPane.vue';
import { toRefs } from 'vue';
import importConfig from './index.ts';

export default {
  name: 'TagsView',
  components: { ScrollPane },
  setup() {
    const {
      base,
      isActive,
      activeStyle,
      isAffix,
      isFirstView,
      isLastView,
      filterAffixTags,
      initTags,
      addTags,
      moveToCurrentTag,
      refreshSelectedTag,
      closeSelectedTag,
      closeRightTags,
      closeLeftTags,
      closeOthersTags,
      closeAllTags,
      toLastView,
      openMenu,
      closeMenu,
      handleScroll,
    } = importConfig();

    return {
      ...toRefs(base),
      isActive,
      activeStyle,
      isAffix,
      isFirstView,
      isLastView,
      filterAffixTags,
      initTags,
      addTags,
      moveToCurrentTag,
      refreshSelectedTag,
      closeSelectedTag,
      closeRightTags,
      closeLeftTags,
      closeOthersTags,
      closeAllTags,
      toLastView,
      openMenu,
      closeMenu,
      handleScroll,
    };
  },
};
</script>

<style lang="less" scoped>
.tags-view-container {
  width: 100%;
  height: 34px;
  background-color: var(--el-bg-color);
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04);
  .tags-view-wrapper {
    .tags-view-item {
      display: inline-block;
      position: relative;
      cursor: pointer;
      height: 26px;
      line-height: 23px;
      background-color: var(--el-bg-color);
      border: 1px solid #ccc;
      color: #495060;
      padding: 0 8px;
      font-size: 12px;
      margin-left: 5px;
      margin-top: 4px;
      text-decoration: none;
      &:hover {
        color: #409eff;
      }
      &:first-of-type {
        margin-left: 15px;
      }
      &:last-of-type {
        margin-right: 15px;
      }
      &.active {
        background-color: #409eff;
        color: #fff;
        border-color: #409eff;
        &::before {
          content: '';
          background: #fff;
          display: inline-block;
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: relative;
          margin-right: 5px;
        }
      }
    }
  }
  .contextmenu {
    margin: 0;
    background: white;
    z-index: 3000;
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
    color: #495060;
    li {
      margin: 0;
      padding: 7px 16px;
      cursor: pointer;
      &:hover {
        background: #eee;
      }
    }
  }
}

:deep(.arco-scrollbar) {
  width: 100%;
  height: 100%;
  white-space: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
}
</style>
